package com.game.majiang.logic.chexuan;

import java.util.ArrayList;
import java.util.List;

import com.game.majiang.algorithm.PaperCard;
import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicPlayer;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.message.proto.out.PaperCardStructrue;
import com.game.server.message.proto.out.PlayerInfo;
import com.game.server.system.AbstractClient;

import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class ChexuanPlayer extends LogicPlayer{
	
	private int nextBobo = 0;//下局需要添加的钵钵
	private int betBobo = 0;//当局已压钵钵 并用于能扣分
	private int scoringBobo = 0;//用于计算 能吃别人的钵钵
	private ArrayList<PaperCard> listHand = new ArrayList<PaperCard>();//手牌
	private int operateType = -1;// 0 1 2 3 4 5 丢 休 敲 大 拖 跟
	private ArrayList<PaperCard> listGroupCard = new ArrayList<PaperCard>();//分组牌 在结算时分清头尾
	private ArrayList<Integer> compareMinUid = new ArrayList<Integer>();//比自己小的玩家
	private ArrayList<Integer> comparePingjuUid = new ArrayList<Integer>();//头尾都平的

	private int winMangguo = 0;//赢得芒果分
	private int mostLoseScore = 0;//最多需要输的分
	private boolean sanhuaBool = false;//有无三花
	private int gameStartMoney = 0;//游戏开始时所拥有的
	private boolean cuopaiBool = false;//搓牌
	private boolean cuopaiTypeBool = false;
	private int[]  brightCard = {0,0};//亮牌
	private int round = 0;
	private ArrayList<PaperCard> listCheatCard = new ArrayList<PaperCard>();
	public ChexuanPlayer(AbstractClient client) {
		super(client);
		// TODO Auto-generated constructor stub
	}

	
	
	public void init() {
		setReady(false);
		this.listHand.clear();
		this.listGroupCard.clear();
		this.operateType = -1;
		this.betBobo = 0;
		this.compareMinUid.clear();
		this.win = 0;
		this.scoringBobo = 0;
		this.winMangguo = 0;
		this.delayedCount = 0;
		this.comparePingjuUid.clear();
		this.mostLoseScore = 0;
		this.sanhuaBool = false;
		this.gameStartMoney = 0;
		this.cuopaiTypeBool = false;
		this.brightCard[0] = 0;
		this.brightCard[1] = 0;
		this.round = 0;
	}
	public void addWin(int score) {
		this.win += score;
	}
	
	public void addMinUid(int uid) {
		if(!this.compareMinUid.contains(uid)) {
			this.compareMinUid.add(uid);
		}
	}
	public void addPingjuUid(int uid) {
		if(!this.comparePingjuUid.contains(uid)) {
			this.comparePingjuUid.add(uid);
		}
	}
	public PaperCard idToGetPaperCard(int cardid) {
		for (PaperCard paperCard : this.listHand) {
			if(paperCard.getId() == cardid) {
				return paperCard;
			}
		}
		return null;
	}
	public ArrayList<PaperCard> idToGetPaperCard(List<Integer> listCardids) {
		ArrayList<PaperCard> listCards = new ArrayList<PaperCard>();
		for (Integer id : listCardids) {
			PaperCard idToGetPaperCard = idToGetPaperCard(id);
			if(idToGetPaperCard != null) {
				if(!listCards.contains(idToGetPaperCard)) {
					listCards.add(idToGetPaperCard);
				}
			}
		}
		return listCards;
	}
	
	
	public PaperCardStructrue[] paperCardToCardStructrue(int uid) {
		PaperCardStructrue[] paperCardToCardStructrue = super.paperCardToCardStructrue(this.listHand);
		if(uid != getUserId()) {
			for (int i = 0; i < paperCardToCardStructrue.length; i++) {
				if(i < 2 ) {
					paperCardToCardStructrue[i].setId(0);
					paperCardToCardStructrue[i].setNum(0);
					paperCardToCardStructrue[i].setType(0);
				}
			}
		}
		return paperCardToCardStructrue;
	}

	/**
	 * 获取玩家在俱乐部的信息
	 * @param uid
	 */
	public XyClubMemberInfo findPlayerClubInfo(int uid){
		XyClubMemberInfo memberInfo = null;
		LogicClient tempClient = (LogicClient) LogicServer.getInstance().getClient(uid);
		XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(tempClient.getClubid());
		if (clubInfo!=null){
			memberInfo = clubInfo.getPlayer().get(uid);
		}
		return memberInfo;
	}


	@Override
	public PlayerInfo toPlayerInfo(int uid) {
		LogicClient tempClient = (LogicClient) getClient();
		PlayerInfo info = new PlayerInfo();
		info.setBobo(this.usebobo);
		info.setHead(tempClient.getVo().getHead());
		info.setId(getUserId());
		if(uid == getUserId()) {
			info.setIntegral(tempClient.getVo().getIntegral());
			info.setZuCards(paperCardToCardStructrue(this.listGroupCard));
		}
		XyClubMemberInfo memberInfo = findPlayerClubInfo(getUserId());
		info.setClubIntegral(memberInfo!=null?memberInfo.getXingyuvalue():0);
		info.setClubRank(memberInfo!=null?memberInfo.getIsManager():3);
		info.setClubid(tempClient.getClubid());
		info.setNickname(tempClient.getVo().getNickName());
		info.setReady(isReady() ? 1 : 0);
		info.setSeatNo(getSeatNo());
		info.setHandCards(paperCardToCardStructrue(uid));
		info.setBetScore(this.betBobo);
		info.setOperationType(this.operateType);
		info.setCuopaiStatus(this.cuopaiBool ? 1 : 0);
		info.setCuopaiType(this.cuopaiTypeBool ? 1 : 0);
		info.setRound(this.round);
		info.setLongitude(tempClient.getVo().getLongitude());
		info.setLatitude(tempClient.getVo().getLatitude());
		if(this.liuZuoEndTime > 0 ) {
			int time = (int) ((this.liuZuoEndTime/1000) - (System.currentTimeMillis() / 1000));
			if(time > 0) {
				info.setLiuzuoTime(time);
			}
		} 
		if(this.setBoboTime > 0) {
			int time = (int) ((this.setBoboTime/1000) + 120 - (System.currentTimeMillis() / 1000));
			if(time > 0) {
				info.setSetBoboTime(time);
			}
		}
		ArrayList<Integer> listBrightCard = new ArrayList<Integer>();
		for (int i = 0; i < this.brightCard.length; i++) {
			if(this.brightCard[i] == 1) {
				listBrightCard.add(i);
			}
		}
		info.setBrightCardsIndex(listBrightCard.toArray(new Integer[listBrightCard.size()]));
		return info;
	}
}
